package edu.hubu.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import edu.hubu.authentication.CustomAuthenticationProvider;
import edu.hubu.commons.JwtUils;
import edu.hubu.dao.impl.*;
import edu.hubu.model.*;
import edu.hubu.service.*;
import edu.hubu.service.impl.TokenService;
import edu.hubu.service.impl.UserService;
import net.moon.jpa.service.DefaultService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy;
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;

/**
 * @author moonlan
 * date 2020/10/23 上午11:46
 */
@Configuration
public class ComponentConfig {

    @Bean
    @ConditionalOnMissingBean
    public BCryptPasswordEncoder encoder() {
        return new BCryptPasswordEncoder();
    }

    public JwtUils jwtUils(UserService userService, TokenService tokenService) {
        return new JwtUils(userService, tokenService);
    }

    @Bean
    @ConditionalOnMissingBean
    public ObjectMapper objectMapper() {
        return new ObjectMapper();
    }

    @Bean
    public DefaultService<AnnouncementType, String> announcementTypeService(IAnnouncementTypeDao announcementTypeDao) {
        return new DefaultService<>(announcementTypeDao, AnnouncementType.class, "announcementTypeIsDeleted", "announcementTypeIsForbidden");
    }

    @Bean
    public DefaultService<Book, String> bookService(IBookDao bookDao) {
        return new DefaultService<>(bookDao, Book.class, "bookIsDeleted", "bookIsForbidden");
    }

    @Bean
    public DefaultService<Announcement, String> announcementService(IAnnouncementDao announcementDao) {
        return new DefaultService<>(announcementDao, Announcement.class, "announcementIsDeleted", "announcementIsForbidden");
    }

    @Bean
    public DefaultService<CourseHour, String> courseHourService(ICourseHourDao courseHourDao) {
        return new DefaultService<>(courseHourDao, CourseHour.class, "courseHourIsDeleted", "courseHourIsForbidden");
    }

    @Bean
    public DefaultService<Course, String> courseService(ICourseDao courseDao) {
        return new DefaultService<>(courseDao, Course.class, "courseIsDeleted", "courseIsForbidden");
    }

    @Bean
    public DefaultService<CourseType, String> courseTypeService(ICourseTypeDao courseTypeDao) {
        return new DefaultService<>(courseTypeDao, CourseType.class, "courseTypeIsDeleted", "courseTypeIsForbidden");
    }

    @Bean
    public DefaultService<Credit, String> creditService(ICreditDao creditDao) {
        return new DefaultService<>(creditDao, Credit.class, "creditIsDeleted", "creditIsForbidden");
    }

    @Bean
    public DefaultService<Institution, String> institutionService(IInstitutionDao institutionDao) {
        return new DefaultService<>(institutionDao, Institution.class, "institutionIsDeleted", "institutionIsForbidden");
    }

    @Bean
    public DefaultService<Major, String> majorService(IMajorDao majorDao) {
        return new DefaultService<>(majorDao, Major.class, "majorIsDeleted", "majorIsForbidden");
    }

    @Bean
    public DefaultService<Permission, String> permissionService(IPermissionDao permissionDao) {
        return new DefaultService<>(permissionDao, Permission.class, "permissionIsDeleted", "permissionIsForbidden");
    }

    @Bean
    public DefaultService<Role, String> roleService(IRoleDao roleDao) {
        return new DefaultService<>(roleDao, Role.class, "roleIsDeleted", "roleIsForbidden");
    }

    @Bean
    public DefaultService<Score, String> scoreService(IScoreDao scoreDao) {
        return new DefaultService<>(scoreDao, Score.class, "scoreIsDeleted", "scoreIsForbidden");
    }

    @Bean
    public DefaultService<StudentCourse, String> studentCourseService(IStudentCourseDao studentCourseDao) {
        return new DefaultService<>(studentCourseDao, StudentCourse.class, "studentCourseIsDeleted", "studentCourseIsForbidden");
    }


    @Bean
    public DefaultService<Student, String> studentService(IStudentDao studentDao) {
        return new DefaultService<>(studentDao, Student.class, "studentIsDeleted", "studentIsForbidden");
    }

    @Bean
    public DefaultService<TeacherCourse, String> teacherCourseService(ITeacherCourseDao teacherCourseDao) {
        return new DefaultService<>(teacherCourseDao, TeacherCourse.class, "teacherCourseIsDeleted", "teacherCourseIsForbidden");
    }

    @Bean
    public DefaultService<Teacher, String> teacherService(ITeacherDao teacherDao) {
        return new DefaultService<>(teacherDao, Teacher.class, "teacherIsDeleted", "teacherIsForbidden");
    }

    @Bean
    public DefaultService<Term, String> termService(ITermDao termDao) {
        return new DefaultService<>(termDao, Term.class, "termIsDeleted", "termIsForbidden");
    }

    @Bean
    public DefaultService<UserClass, String> userClassService(IUserClassDao userClassDao) {
        return new DefaultService<>(userClassDao, UserClass.class, "userClassIsDeleted", "userClassIsForbidden");
    }
}
